package com.ITArmy.service.impl;

import com.ITArmy.beans.*;
import com.ITArmy.beans.Shows.UserComment;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.*;
import com.ITArmy.service.CommentService;
import com.ITArmy.util.CheckMessage;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    SerializeMapper serializeMapper;
    @Autowired
    ChapterMapper chapterMapper;
    @Autowired
    UserMapper userMapper;
    GetUserSession getUserSession = new GetUserSession();

    /**
     * 用户进行评论博客、（回答）问题、评论连载
     */
    @Override
    public ServerResponse comment(HttpServletRequest request, Comment comment, Model model) {
        User user = getUserSession.getUser(request);
        //存入uid
        comment.setuId(user.getuId());
        //存入时间
        comment.setcTime(new Date());
        //增加评论
        int resultCount = commentMapper.insertSelective(comment);
        if (resultCount == 1) {
            //判断24小时之内评论的数量
            Date enddate=comment.getcTime();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(enddate);
            calendar.add(Calendar.DATE, -1);
            Date startdate=calendar.getTime();
            int count=commentMapper.findCommentBetweenTime(comment.getuId(),startdate,enddate);
            //如果评论数小于10，则金币加一
            if (count<=10){
                //金币+1
                userMapper.addGold(1, user.getuId());
                model.addAttribute("boolean",true);
            }else {
                model.addAttribute("boolean",false);
            }
            Message message = new Message();
            //消息中存入uid
            message.setuId(user.getuId());
            if (comment.getbId() != null) {
                //消息中存入被作用者id
                message.setuCid(blogMapper.selectByPrimaryKey(comment.getbId()).getuId());
                Comment c = commentMapper.findCommentByUidAndTime(user.getuId(), comment.getcTime());
                //消息中存入评论id
                message.setcId(c.getcId());
            } else if (comment.getqId() != null) {
                //消息中存入被作用者id
                message.setuCid(questionMapper.selectByPrimaryKey(comment.getqId()).getaUId());
                Comment c = commentMapper.findCommentByUidAndTime(user.getuId(), comment.getcTime());
                //消息中存入评论id
                message.setcId(c.getcId());
            } else if (comment.getChId() != null) {
                Chapter chapter = chapterMapper.selectByPrimaryKey(comment.getChId());
                //消息中存入被作用者id
                message.setuCid(serializeMapper.selectByPrimaryKey(chapter.getsId()).getuId());
                Comment c = commentMapper.findCommentByUidAndTime(user.getuId(), comment.getcTime());
                //消息中存入评论id
                message.setcId(c.getcId());
            } else if (comment.getsId() != null) {
                //消息中存入被作用者id
                message.setuCid(serializeMapper.selectByPrimaryKey(comment.getsId()).getuId());
                Comment c = commentMapper.findCommentByUidAndTime(user.getuId(), comment.getcTime());
                //消息中存入评论id
                message.setcId(c.getcId());
            }
            message.setTime(new Date());
            messageMapper.insertSelective(message);
        }
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "评论成功", "评论失败");
    }

    /**
     * 用户进行删除评论
     */
    @Override
    public ServerResponse deleteComment(long commentId) {
        //删除评论信息
        int resultCount = commentMapper.deleteByPrimaryKey(commentId);
        //删除消息信息
        messageMapper.deleteMessageByCid(commentId);
        //删除

        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "删除成功", "删除失败");
    }

    /**
     * 根据连载的id进行查询评论（封装到UserComment里面）
     */
    @Override
    public List<UserComment> findCommentBySerialize(long serializeId) {
        //查询出连载所有的评论信息
        List<Comment> commentList = commentMapper.findCommentBySerialize(serializeId);
        List<UserComment> userComments = new ArrayList<UserComment>();
        for (int i = 0; i < commentList.size(); i++) {
            Comment comment = commentList.get(i);
            userComments.add(new UserComment(comment, userMapper.selectByPrimaryKey(comment.getuId())));
        }
        return userComments;
    }

    /**
     * 根据博客id查找评论
     */
    @Override
    public List<Comment> findCommentByBlogId(Long bid) {
        return commentMapper.findCommentByBlogId(bid);
    }
}
